﻿using System;
using System.Text.RegularExpressions;

namespace Util.TitleParser
{
    //Format 01x08 and 1x08
    class Parse_sxee : ITitleParser
    {
        public void ParseTitle(string Title, out string Name, out string Details, out long Season, out long Episode)
        {
            Match m = Regex.Match(Title, ".[0-9]x[0-3][0-9]");
            if (!m.Success)
            {
                throw new ArgumentException("Couldn't parse Title", "Title");
            }

            int firstLetterAdjustment = 0;
            if (m.Value[0] != '0' && m.Value[0] != '1' && m.Value[0] != '2')
            {
                firstLetterAdjustment = 1;
            }

            Season = long.Parse(m.Value.Substring(firstLetterAdjustment, 2 - firstLetterAdjustment), System.Globalization.CultureInfo.InvariantCulture);
            Episode = long.Parse(m.Value.Substring(3, 2), System.Globalization.CultureInfo.InvariantCulture);

            try
            {
                Name = Title.Substring(0, m.Index - 1 + firstLetterAdjustment);
            }
            catch (Exception)
            {
                Name = Title;
            }

            try
            {
                Details = Title.Substring(m.Index + 5);
            }
            catch (Exception)
            {
                Details = Title;
            }
        }
    }
}
